package search.binary_search;

/**
 * 二分查找 有重复元素，查找第一个大于等于给定值的元素
 *
 * @author yeyangtao created at 11:36 2020/9/29
 */
public class BinarySearch3 {
    public static int binarySearch(int[] a, int value) {
        int size = a.length;
        int low = 0;
        int high = size - 1;
        while (low <= high) {
            int mid = low + ((high - low) >> 1);
            if (a[mid] >= value)
                if (mid == 0 || a[mid - 1] < value) return mid;
                else high = mid - 1;
            else
                low = mid + 1;
        }
        return -1;
    }
}
